package com.scorpio.goods.service.impl;

import com.scorpio.common.exception.CustomException;
import com.scorpio.common.service.impl.BaseServiceImpl;
import com.scorpio.goods.domain.GoodsSkuSpecEntity;
import com.scorpio.goods.mapper.GoodsSkuSpecMapper;
import com.scorpio.goods.service.IGoodsSkuSpecService;
import com.scorpio.goods.service.IGoodsSpecService;
import com.scorpio.redis.cachelock.RedisLock;
import com.scorpio.redis.cachelock.RedisLockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * sku和规格 关联Service业务层处理
 *
 * @author scorpio
 * @date 2021-11-05
 */
@Service
public class GoodsSkuSpecServiceImpl extends BaseServiceImpl<GoodsSkuSpecMapper, GoodsSkuSpecEntity> implements IGoodsSkuSpecService {

    @Autowired
    private RedisLock redisLock;

    @Autowired
    private IGoodsSpecService goodsSpecService;

    @Override
    public List<Long> selectSkuIdsBySpec(Long specId) {
        return baseDao.selectSkuIdsBySpec(specId);
    }

    @Override
    public List<Long> selectSkuIdsBySku(Long specId) {
        return baseDao.selectSkuIdsBySku(specId);
    }

    @Override
    public List<GoodsSkuSpecEntity> selectSkuBySpec(Long specId) {
        return baseDao.selectSkuBySpec(specId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(String goodsNumber, GoodsSkuSpecEntity entity) {
        String lockKey = "GoodsSkuSpecServiceImpl_save:" + entity.getGoodsId();
        if (redisLock.lock(lockKey, 5)) {
            try {
                GoodsSkuSpecEntity xEntity = baseDao.selectByName(entity.getGoodsId(), entity.getSkuId(), entity.getSpecName());
                int count = baseDao.insert(entity);
                if (count > 0) {
                    if (xEntity == null) {
                        int number = 101 + baseDao.couneSkuByGoods(entity.getGoodsId());
                        for (int i = 0; i < 20; i++) {
                            if (baseDao.countByNumber(entity.getGoodsId(), "" + number) > 0 || goodsSpecService.countByNumber(goodsNumber + number) > 0) {
                                number++;
                            } else {
                                break;
                            }
                            if (i >= 19) {
                                throw new CustomException("未知错误,请联系管理员操作");
                            }
                        }
                        if (baseDao.updateSkucNumber(entity.getId(), "" + number) == 0) {
                            throw new CustomException("未知错误,请联系管理员操作");
                        }
                        entity.setSpecNumber("" + number);
                    } else {
                        if (baseDao.updateSkucNumber(entity.getId(), xEntity.getSpecNumber()) == 0) {
                            throw new CustomException("未知错误,请联系管理员操作");
                        }
                        entity.setSpecNumber(xEntity.getSpecNumber());
                    }
                }
                return count;
            } finally {
                redisLock.unlock(lockKey);
            }
        }
        throw new RedisLockException();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAll(List<GoodsSkuSpecEntity> list) {
        return insertBatch(list);
    }
}
